import request from 'Utils/request'

/**
 * Get Music Details excluding lyrics.
 * http://m.kugou.com/app/i/getSongInfo.php?cmd=playInfo&hash=CB7EE97F4CC11C4EA7A1FA4B516A5D97
 * @param {String} hash the unique sign of music.
 */
export function getSongInfo (hash) {
    return new Promise((resolve, reject) => {
        request
            .get(`/mApi/app/i/getSongInfo.php?cmd=playInfo&hash=${hash}`)
            .then(data => {
                resolve(data)
            })
            .catch(err => {
                reject(err)
            })
    })
}

/**
 * Get Music Details including lyrics.
 * http://www.kugou.com/yy/index.php?r=play/getdata&hash=CB7EE97F4CC11C4EA7A1FA4B516A5D97
 * @param {String} hash the unique sign of music.
 */
export function getSongDetails (hash) {
    return new Promise((resolve, reject) => {
        request
            .get(`/Api/yy/index.php?r=play/getdata&hash=${hash}`)
            .then(data => {
                resolve(data)
            })
            .catch(err => {
                reject(err)
            })
    })
}

/**
 * Get Hot Search List
 * http://mobilecdn.kugou.com/api/v3/search/hot?format=json&plat=0&count=30
 * @param {Number} plat Start Number
 * @param {Number} count Hot Search List Length
 */
export function getSearchHot (plat, count) {
    return new Promise((resolve, reject) => {
        request
            .get(`/cdnApi/api/v3/search/hot?format=json&plat=${plat}&count=${count}`)
            .then(data => {
                resolve(data)
            })
            .catch(err => {
                reject(err)
            })
    })
}

/**
 * Search Music
 * http://mobilecdn.kugou.com/api/v3/search/song?format=json&keyword=%E7%8E%8B%E5%8A%9B%E5%AE%8F&page=1&pagesize=20&showtype=1
 * @param {String} keywords Keywords of Search
 * @param {Number} page List Page Number
 * @param {Number} pagesize Length of page
 * @param {Number} showtype
 */
export function search (keywords, page, pagesize, showtype = 1) {
    return new Promise((resolve, reject) => {
        request
            .get(`/cdnApi/api/v3/search/song?format=json&keyword=${keywords}&page=${page}&pagesize=${pagesize}&showtype=${showtype}`)
            .then(data => {
                resolve(data)
            })
            .catch(err => {
                reject(err)
            })
    })
}

/**
 * Get song list.
 * http://m.kugou.com/plist/index&json=true
 */
export function getSongList () {
    return new Promise((resolve, reject) => {
        request
            .get('/mApi/plist/index&json=true')
            .then(data => {
                resolve(data)
            })
            .catch(err => {
                reject(err)
            })
    })
}

/**
 * Get details of song list based on specialid.
 * http://m.kugou.com/plist/list/125032?json=true
 * @param {String} specialid special id.
 */
export function getSongListDetails (specialid) {
    return new Promise((resolve, reject) => {
        request
            .get(`/mApi/plist/list/${specialid}?json=true`)
            .then(data => {
                resolve(data)
            })
            .catch(err => {
                reject(err)
            })
    })
}
